package com.szqhyxj.youxiangju.biz.policy;

import com.szqhyxj.youxiangju.biz.personalInformation.PersonalInformationModel;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.security.Policy;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/10/30.
 */

public class PolicyPresenter implements PolicyContract.Presenter {

    private PolicyContract.View view;

    public PolicyPresenter(PolicyContract.View view) {
        this.view = view;

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void loadPolicyInformation() {
        PolicyModel.getPolicy()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<PolicyModel.PolicyEntity>>() {
                    @Override
                    public void accept(BaseResult<PolicyModel.PolicyEntity> policyEntityBaseResult) throws Exception {
                        if (policyEntityBaseResult.businessLogicSuccess()) {
                            int insuranceType = policyEntityBaseResult.getData().getInsuranceType();
                            if (insuranceType == 0) {
                                view.showPolicybackground();
                            } else if (insuranceType == 1) {
                                view.enShowPolicybackground();

                            }
                            view.showInsuranceno(policyEntityBaseResult.getData().getInsuranceNo());
                            view.showTerminsurance(policyEntityBaseResult.getData().getInsuranceTerm()+"年");
                            view.showEffectivedate(policyEntityBaseResult.getData().getEffectiveDate());
                            view.showEnddate(policyEntityBaseResult.getData().getEndDate());
                            view.showInsured(policyEntityBaseResult.getData().getInsurant());
                            if (policyEntityBaseResult.getData().getDocumentType()==1){
                                view.showCertificatetype("身份证");
                            }
                            view.showIdnumber(policyEntityBaseResult.getData().getIdentNumber());
                            view.showInsurancelimit(policyEntityBaseResult.getData().getGuaranteeAmount());
                            view.showInsurance("￥" + policyEntityBaseResult.getData().getInsuranceAmount()+".00");
                            view.showInsuranceobject(policyEntityBaseResult.getData().getInsurancePerson());
                            view.showEneficiary(policyEntityBaseResult.getData().getInsurant());
                        } else {
                            view.showFail(policyEntityBaseResult.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showFail(throwable.getMessage());
                    }
                });

    }
}
